package com.dingwei.superiordiary.parser;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.ui.activity.LookUserCenterActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.T;

import org.xml.sax.XMLReader;

import java.util.Map;

import utils.HtmlParserUtil;

/**
 * Project ID：400YF17051<br/>
 * Resume:
 *
 * @author 汪波
 * @version 1.0
 * @see
 * @since 2017/4/15 汪波 first commit
 */
public class HtmlTagHandler implements Html.TagHandler {
    private static final String TAG = "tag";
    private static final String USER = "user";
    private static final String ID = "id";
    public static final String NAME = "name";

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.toLowerCase().equals(TAG) || tag.toLowerCase().equals(USER)) {
            if (opening) {
                Map<String, String> map = HtmlParserUtil.parseStart(tag, output, xmlReader);
                String id = map.get(ID);
                String name = map.get(NAME);
                output.setSpan(new TagBean(name, id), output.length(), output.length(), Spannable.SPAN_MARK_MARK);
            } else {
                endTag(tag, output, xmlReader);
            }
        }
    }

    private Object getLast(Spanned text, Class kind) {
        /*
         * This knows that the last returned object from getSpans()
         * will be the most recently added.
         */
        Object[] objs = text.getSpans(0, text.length(), kind);

        if (objs.length == 0) {
            return null;
        } else {
            return objs[objs.length - 1];
        }
    }

    private void endTag(String tag, Editable text, XMLReader xmlReader) {
        //myfont标签不能裸着，即必须有html等标签包裹，或者前面有其他内容，否则字体大小不能起作用
        //即getlast变成从后面取，最后的内容的范围是0到文本全长度
        int len = text.length();
        Object obj = getLast(text, TagBean.class);
        int where = text.getSpanStart(obj);
        text.removeSpan(obj);
        Log.e("AAA", "where:" + where + ",len:" + len);
        if (where != len) {
            final TagBean t = (TagBean) obj;

            if (null != t) {
                text.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
//                        T.showShort(BaseActivity.context,t.getId());
                        Intent intent = new Intent(BaseActivity.context, LookUserCenterActivity.class);
                        intent.putExtra("user_id",t.getId());
                        BaseActivity.context.startActivity(intent);
                    }
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(ContextCompat.getColor(BaseActivity.context, R.color.selected_bg_color));       //设置文件颜色
                        ds.setUnderlineText(false);      //设置下划线
                    }
                }, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}